Welcome back. So, we've learned how to do the dictionary accumulation pattern,
but now we want to learn some things that we
might want to do with our accumulated dictionary.
So, rarely do we just want to do something like
accumulate the number of every character in a dictionary,
we typically want to actually do something with that data.
So, here we have the same code as before.
So, we open up the file scarlet.txt,
load its contents into the variable txt,
and initialize our accumulator variable,
letter counts, to be an empty dictionary.
Then through this for-loop,
we associate every key in letter counts,
which is going to be a character with the number of
times that character appears in scarlet.txt.
So, in other words, by the time we get to line 12,
then letter counts is going to be a dictionary where
every key is going to be a character such as 't',
and every value is going to be the number of times that character appeared.
So in the case of 't', it's 17,584.
So, in our code before,
we just printed out the number of 't's'
by printing out "There are" and then something like,
letter count sub 't', 't's' in scarlet.txt.
We can do the same thing with other characters of course,
so we can say that there are this many 'a's' and 'b's' and so on.
But suppose that we wanted to print out the number of
every single character in letter counts.
Well, we could do that by using a for-loop.
So, we could say for every character,
I'll just call my iterator variable 'y' in letter counts.
Then inside of our for-loop we can just say "There are" and then letter counts
sub y and we can say that there are that many of the character y.
So, what this code is going to do,
is it's going to loop through all of the keys in letter counts and for every key,
y is going to be the value of that key and
then we're going to get the number of times that character y
appears by printing out letter count sub y and then we say
"There are" that many and then whatever that character y is.
So, when we run our code,
we can see that there are this many t's,
this many s's, this many spaces,
this many capital 't's' and so on.
You can see that there are quite a few characters in scarlet.txt.
So, let's suppose that we had called our dictionary x instead of letter counts.
In this question we're asked,
which of the following will print out true if there are more occurrences
of the character e than the character t in the text,
A Study in Scarlet,
and false if t occurred more frequently?
So, this is assuming that our previous code has run,
except our dictionary is called x instead of letter counts.
So, we want to write an expression that's going to be
true if there are more e's than t's.
So the way that we do that,
we get the number of the character e by saying, x sub 'e',
this is going to get the value associated with the character e in the dictionary x,
and then we get the number of t's by saying, x sub 't'.
If we want to know if there are more e's than t's,
then we can write the expression,
x sub 'e' is greater than x sub 't'.
So in other words, the answer here is going to be B.
So, if you've ever played the game Scrabble,
then you know that different letters in Scrabble have different scores.
So, letters that are more rare have a higher score than letters that are more common.
So, let's suppose that we want to open up
scarlet2.txt and we want to figure out what's the "Scrabble score" for this.
So in other words, for every character,
suppose that we want to know not just how many times that character appeared,
but what the Scrabble score for that character with be.
So, from lines one through 11,
we have the dictionary accumulation pattern and it accumulates the frequency of
every character in scarlet2.txt into this dictionary x.
Then on line 13,
we have a different dictionary,
which represents the Scrabble letter value of every character.
So in 'a', which is a really common letter,
only has a score of one.
Whereas a 'z', which is a lot less common,
has a Scrabble value of 10.
So, if we wanted to get the Scrabble score in scarlet2.txt,
then what we could do is we could loop through
every character inside of the dictionary letter values.
So I'll say, for every character,
I'll call it y in x.
Then because not every character has a Scrabble score, so for instance,
numbers or exclamation points don't have Scrabble scores but they're in our dictionary,
we want to first check to see is that character y in letter values.
So, I'll say if y is in letter values.
We want to keep track of what's our letter values so far,
so the way that we do that is with the standard accumulation pattern.
So, I'm going to initialize an accumulator variable,
I'll call it scrabble score,
we initialize it to zero,
and then we say, if this character y has a letter value,
then scrabble score equals its previous score.
So, scrabble score equals scrabble score plus,
and then we want to add the score for that letter.
So in other words,
'a' has value one,
'b' has value three,
'c' has value three, and so on.
So we get that score by saying letter values sub y,
again y is going to be a, b,
c, d, e, f,
g, and so on.
Then we want to multiply that by the number of times that that character appears.
So, I'll say letter values sub y,
times x sub y,
x is again our dictionary accumulator variable from the previous problem.
So in other words, we're saying the score is
the previous score plus the value of that letter,
times the number of times that that letter appeared.
So, this is using the standard accumulation pattern.
Here, our accumulator variable is scrabble score and we update it by saying,
scrabble score equals scrabble score plus letter value sub y times x sub y.
So, if I print out the scrabble score at the end of this for-loop,
then I should expect it to be the actual scrabble score of scarlet2.txt.
You can see that this has a pretty high scrabble score overall.
Let's do some more questions that involve dictionary accumulation
and doing something after we accumulate the results from the dictionary.
So, in this question,
we're told the dictionary travel contains the number
of countries within each continent that Jackie has traveled to,
and we're asked to find the total number of countries that Jackie has been to.
In other words, our result is going to be two plus eight plus three plus four and so on.
We're asked to save that into the variable named "total".
So, in this question,
we're given a dictionary and it doesn't matter how we arrived at this dictionary.
So, we might have 20 lines before this that compute
this dictionary's value or in this case we're
just given the dictionary as a literal object.
But regardless of how we get that dictionary,
what we want to do is we want to accumulate the sum
of every value associated with every key in that dictionary.
So, we can do that with the standard accumulation pattern.
So, I'm going to first initialize an accumulator variable total to be zero,
so this is initializing our accumulator variable.
Then we want to loop through every single continent, so I'll say,
for continent in travel.
So, continent is going to be North America,
Europe, South America, Asia, Africa et cetera.
So, for every continent in travel,
we get the number of countries within that continent that Jackie has
been to by saying, travel sub continent.
We want to say total equals total plus travel sub continent,
so this is us updating our accumulator variable.
So again, what we're doing is we're looping through every key in
our dictionary and we are getting the value associated with that key.
So, two, eight, three and so on,
and adding that value to our previous total.
By the time we're done with that for-loop,
we should have the total number of countries that Jackie has been to.
In this question, we're told that schedule is a dictionary where
a class name is the key and its value is how many credits it's worth.
We're asked to go through and accumulate the total number of credits that have
been earned so far and assign that to the variable total credits.
So, this is the same idea with just a slightly different dictionary.
So, our accumulator variable is going to be named total credits.
We're going to initialize it to zero,
then we want to loop through every course in our schedule.
So, I'll say for course in schedule.
So again here, course is going to be "UARTS 150",
"SPANISH 103", "English 125" and so on.
We get the number of credits for that course by saying,
schedule sub course and we add that to the total number of credits by saying,
total credits equals total credits
plus schedule sub course.
That's all for now, until next time.